/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple.mavuno;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

import edu.cmu.lti.dimple.FileUtils;
import edu.cmu.lti.dimple.nlp.AbstractNLPTool;
import edu.cmu.lti.dimple.nlp.EnglishNLP;

/**
 * Util class for this package.
 * 
 * @author Hideki Shima
 *
 */
public class MavunoUtil {

  private static AbstractNLPTool nlpTool = new EnglishNLP(); 
  
  public static Pattern createPattern( List<String> terms, 
          String algorithm, int listSize, boolean removeNoContentWordPattern ) {
    StringBuilder sb = new StringBuilder();
    StringBuilder removed = new StringBuilder();
    for (int i=0; i<Math.min(listSize, terms.size()); i++) {
      String term = terms.get(i);
      List<String> contentWords = nlpTool.extractContentWords(term);
      if (contentWords.size()==0 || contentWords.get(0).length()==0) {
        removed.append(removed.length()==0?"":"|");
        removed.append(term);
        continue;
      }
      sb.append(sb.length()==0?"":"|");
      sb.append(Pattern.quote(term));
    }
    if (removed.length()>0) {
      FileUtils.appendFile("Invalid patterns are not included: "+removed+"\n", 
        new File("target/logs/unused_"+algorithm+".txt"));
    }
    
    return sb.toString().length()!=0 
    ? Pattern.compile("\\b("+sb.toString()+")\\b",Pattern.CASE_INSENSITIVE)
    : null;
  }
  
}
